package com.test.java;

import java.util.Scanner;

/**
 * 给定任一个各位数字不完全相同的4位正整数，如果我们先把4个数字按非递增排序，再按非递减排序，
 * 然后用第1个数字减第2个数字，将得到一个新的数字。一直重复这样做，我们很快会停在有“数字黑洞”
 * 之称的6174，这个神奇的数字也叫Kaprekar常数。
 * =============================================================
 * Version          Author          Date            Description
 * 1.0              白建         2017/12/19             创建
 * =============================================================
 */
public class Java_10
{

    private static char[] ORIGINAL;

    private static char[] DESC;

    private static char[] ASC;

    private static char[] RESULT;

    public static void main(String[] args)
    {

        init();
        process();
    }

    private static void init()
    {

        ORIGINAL = new char[4];
        DESC = new char[4];
        ASC = new char[4];
        RESULT = new char[4];

        Scanner scanner = new Scanner(System.in);
        int temp = scanner.nextInt();
        parseCharArray(temp, ORIGINAL);
        calculate();
    }

    private static void parseCharArray(int temp, char[] array)
    {

        for (int i = 0; i < 4; i++)
        {
            String tempString = String.valueOf((int) (temp / Math.pow(10, 3 - i)));
            array[i] = tempString.charAt(tempString.length() - 1);
        }
    }

    private static void calculate()
    {

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3 - i; j++)
            {
                if (ORIGINAL[j] > ORIGINAL[j + 1])
                {
                    char temp = ORIGINAL[j + 1];
                    ORIGINAL[j + 1] = ORIGINAL[j];
                    ORIGINAL[j] = temp;
                }
            }
        }
        for (int i = 0; i < 4; i++)
        {
            ASC[i] = ORIGINAL[i];
            DESC[i] = ORIGINAL[3 - i];
        }
        int result = Integer.parseInt(String.valueOf(DESC)) - Integer.parseInt(String.valueOf(ASC));
        parseCharArray(result, RESULT);
    }

    private static void process()
    {

        while (!"6174".equalsIgnoreCase(String.valueOf(RESULT)))
        {
            if ("0000".equalsIgnoreCase(String.valueOf(RESULT)))
            {
                System.out.println(String.valueOf(DESC) + " - " + String.valueOf(ASC) + " = " + String.valueOf(RESULT));
                return;
            }
            else
            {
                System.out.println(String.valueOf(DESC) + " - " + String.valueOf(ASC) + " = " + String.valueOf(RESULT));
                for (int i = 0; i < 4; i++)
                {
                    ORIGINAL[i] = RESULT[i];
                }
                calculate();
            }
        }
        System.out.println(String.valueOf(DESC) + " - " + String.valueOf(ASC) + " = " + String.valueOf(RESULT));
    }
}
